package pingan.com.imandtwo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

import java.util.List;

import chendemin.bwei.com.myapplication.gen.DbBeanDao;
import pingan.com.imandtwo.R;
import pingan.com.imandtwo.db.DbBean;
import pingan.com.imandtwo.db.DbUtils;

/**
 * Created by xsj on 2018/2/9.
 */

public class AddFriendAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<DbBean> list ;
    private Context context;

    // 多条目的两种类型
    private static final int TYPE_HAS_PIC = 0;
    private static final int TYPE_NO_PIC = 1;
    private final DbBeanDao dbDao;


    public AddFriendAdapter(List<DbBean> list, Context context) {
        this.list = list;
        this.context = context;
        dbDao = DbUtils.getSingleton().getDbDao(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == 0){
            //第一种
            View view = View.inflate(context, R.layout.addfrienditem1, null);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }else if(viewType == 1){
            //第二种
            View view = View.inflate(context, R.layout.addfrienditem2, null);
            ViewHolder1 holder1 = new ViewHolder1(view);
            return holder1;
        }else{
            View view = View.inflate(context, R.layout.addfrienditem1, null);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof ViewHolder){
            ViewHolder h = (ViewHolder) holder;
            h.name.setText(list.get(position).getName());
            h.message.setText(list.get(position).getMessage());
            h.tongyi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        EMClient.getInstance().contactManager().acceptInvitation(list.get(position).getName());
                        Toast.makeText(context,"添加成功",Toast.LENGTH_SHORT).show();
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                    }
                }
            });
            h.jujue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        EMClient.getInstance().contactManager().declineInvitation(list.get(position).getName());
                        Toast.makeText(context,"已拒绝好友请求",Toast.LENGTH_SHORT).show();
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                    }
                }
            });
            h.backname.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        EMClient.getInstance().contactManager().addUserToBlackList(list.get(position).getName(),true);
                        Toast.makeText(context,"拉入黑名单，从此无骚扰",Toast.LENGTH_SHORT).show();
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                    }
                }
            });
        }else if(holder instanceof ViewHolder1){
            ViewHolder1 h1 = (ViewHolder1) holder;
            h1.name.setText(list.get(position).getName());
            h1.message.setText(list.get(position).getMessage());
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(list.get(position).getType()==false){
            return TYPE_HAS_PIC;
        }else{
            return TYPE_NO_PIC;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        private TextView name,message;
        private Button jujue,backname,tongyi;

        public ViewHolder(View itemView) {
            super(itemView);
            name=(TextView) itemView.findViewById(R.id.name);
            message=(TextView)itemView.findViewById(R.id.message);
            tongyi=(Button)itemView.findViewById(R.id.tongyi);
            jujue=(Button)itemView.findViewById(R.id.jujue);
            backname=(Button)itemView.findViewById(R.id.backname);
        }
    }

    class ViewHolder1 extends RecyclerView.ViewHolder{

        private TextView name,message;
        private Button jujue,backname,tongyi;

        public ViewHolder1(View itemView) {
            super(itemView);
            name=(TextView)itemView.findViewById(R.id.name);
            message=(TextView)itemView.findViewById(R.id.message);
            tongyi=(Button)itemView.findViewById(R.id.tongyi);
            jujue=(Button)itemView.findViewById(R.id.jujue);
            backname=(Button)itemView.findViewById(R.id.backname);
        }
    }
}
